package view;
import java.awt.*;
import java.awt.image.*;
import javax.swing.JComponent;
public class VolatileImageMaker extends JComponent
{

	private static final long serialVersionUID = 1L;
private VolatileImage backBuffer;
private Image myImage;
// more code to write here ...


private void createBackBuffer()
{
// get the actual GraphicsConfiguration and create a compatible
// VolatileImage as BackBuffer
GraphicsConfiguration gc = getGraphicsConfiguration();
backBuffer = gc.createCompatibleVolatileImage(800,500);
}
public void renderScreen()
{
// if backBuffer doesn't exist, create one
if (backBuffer == null) createBackBuffer();
do
{
// validate the backBuffer
int valCode = backBuffer.validate(getGraphicsConfiguration());
if (valCode == VolatileImage.IMAGE_RESTORED)
{
System.out.println("backBuffer - IMAGE_RESTORED");
// This case is just here for illustration
// purposes. Since we are
// recreating the contents of the back buffer
// every time through this loop, we actually
// do not need to do anything here to recreate
// the contents. If our VImage was an image that
// we were going to be copying _from_, then we
// would need to restore the contents at this point
}
else if (valCode == VolatileImage.IMAGE_INCOMPATIBLE)
{
// backBuffer Image is incompatible with actual screen
// settings, so we have to create a new compatible one
System.out.println("backBuffer - IMAGE_INCOMPATIBLE");
createBackBuffer();
}

// get Graphics object from backbuffer
Graphics g = backBuffer.getGraphics();
// render on backbuffer

g.drawImage(myImage,0,0,this);
g.drawLine(0,0,10,20);
// ...
// rendering is done; now check if contents got lost
// and loop if necessary
} while (backBuffer.contentsLost());
}
public VolatileImage getImage(){
	return this.backBuffer;
}

// ... more code to write here
}